<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | 王勇程 <laughskydragon@qq.com>                         |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\admin\controller;

/**
 * 定时任务控制器（手动触发）
 * 注意：自动定时任务请使用命令行：php think vip:task
 * @author 王勇程 <laughskydragon@qq.com>
 */
class Task extends AdminBase
{
    /**
     * 手动处理过期VIP用户
     * 访问地址：/admin.php/Task/processExpiredVip.html
     */
    public function processExpiredVip()
    {
        try {
            $result = $this->logicTask->processExpiredVip();
            
            if ($result[0] == RESULT_SUCCESS) {
                return json(['code' => 1, 'msg' => $result[1], 'data' => []]);
            } else {
                return json(['code' => 0, 'msg' => $result[1], 'data' => []]);
            }
        } catch (\Exception $e) {
            \think\facade\Log::error('处理过期VIP用户失败：' . $e->getMessage());
            return json(['code' => 0, 'msg' => '处理过期VIP用户失败', 'data' => []]);
        }
    }
    
    /**
     * 手动发送VIP过期提醒
     * 访问地址：/admin.php/Task/sendVipExpireReminder.html
     */
    public function sendVipExpireReminder()
    {
        try {
            $result = $this->logicTask->sendVipExpireReminder();
            
            if ($result[0] == RESULT_SUCCESS) {
                return json(['code' => 1, 'msg' => $result[1], 'data' => []]);
            } else {
                return json(['code' => 0, 'msg' => $result[1], 'data' => []]);
            }
        } catch (\Exception $e) {
            \think\facade\Log::error('发送VIP过期提醒失败：' . $e->getMessage());
            return json(['code' => 0, 'msg' => '发送VIP过期提醒失败', 'data' => []]);
        }
    }
    
    /**
     * 手动清理过期数据
     * 访问地址：/admin.php/Task/cleanupExpiredData.html
     */
    public function cleanupExpiredData()
    {
        try {
            $result = $this->logicTask->cleanupExpiredData();
            
            if ($result[0] == RESULT_SUCCESS) {
                return json(['code' => 1, 'msg' => $result[1], 'data' => []]);
            } else {
                return json(['code' => 0, 'msg' => $result[1], 'data' => []]);
            }
        } catch (\Exception $e) {
            \think\facade\Log::error('清理过期数据失败：' . $e->getMessage());
            return json(['code' => 0, 'msg' => '清理过期数据失败', 'data' => []]);
        }
    }
    
    /**
     * 手动执行所有定时任务
     * 访问地址：/admin.php/Task/runAllTasks.html
     */
    public function runAllTasks()
    {
        try {
            $messages = [];
            
            // 处理过期VIP用户
            $expiredResult = $this->logicTask->processExpiredVip();
            $messages[] = '过期VIP处理：' . $expiredResult[1];
            
            // 发送VIP过期提醒
            $reminderResult = $this->logicTask->sendVipExpireReminder();
            $messages[] = 'VIP过期提醒：' . $reminderResult[1];
            
            // 清理过期数据
            $cleanupResult = $this->logicTask->cleanupExpiredData();
            $messages[] = '数据清理：' . $cleanupResult[1];
            
            return json(['code' => 1, 'msg' => '所有任务执行完成', 'data' => $messages]);
            
        } catch (\Exception $e) {
            \think\facade\Log::error('执行定时任务失败：' . $e->getMessage());
            return json(['code' => 0, 'msg' => '执行定时任务失败', 'data' => []]);
        }
    }
    
    /**
     * 获取任务执行状态
     * 访问地址：/admin.php/Task/getTaskStatus.html
     */
    public function getTaskStatus()
    {
        try {
            $result = $this->logicTask->getTaskStatus();
            
            if ($result[0] == RESULT_SUCCESS) {
                return json(['code' => 1, 'msg' => $result[1], 'data' => $result[2] ?? []]);
            } else {
                return json(['code' => 0, 'msg' => $result[1], 'data' => []]);
            }
        } catch (\Exception $e) {
            \think\facade\Log::error('获取任务状态失败：' . $e->getMessage());
            return json(['code' => 0, 'msg' => '获取任务状态失败', 'data' => []]);
        }
    }
}